package zzs.generalframework.console.core.user.userdetailinner.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import zzs.generalframework.common.core.user.userdetailinner.UserDetailInnerBO;
import zzs.generalframework.common.core.user.userdetailinner.UserDetailInnerInfo;
import zzs.generalframework.common.core.user.userdetailinner.UserDetailInnerPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.user.userdetailinner.dao.IUserDetailInnerDao;
import zzs.generalframework.console.core.user.userdetailinner.service.IUserDetailInnerService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.user.userdetailinner.service.impl
 * @author zzs
 * @date 2018年2月5日
 * @version 1.0
 */
public class UserDetailInnerServiceImpl implements IUserDetailInnerService
{

	private Logger logger = LoggerFactory.getLogger(UserDetailInnerServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IUserDetailInnerDao userDetailInnerDao;

	@Override
	public ConsoleReturnBO saveUserDetailInner(UserDetailInnerBO userDetailInnerBO)
	{
		logger.debug("====保存用户详细(内部)信息开始====");
		logger.debug("====保存用户详细(内部)信息.入参:UserDetailInnerBO=[{}]====", userDetailInnerBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserDetailInnerPO userDetailInnerPO = new UserDetailInnerPO();
		copyUserDetailInnerBO2UserDetailInnerPO(userDetailInnerBO, userDetailInnerPO);
		Boolean insertResult = userDetailInnerDao.insert(userDetailInnerPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存用户详细(内部)信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存用户详细(内部)信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveUserDetailInner(List<UserDetailInnerBO> userDetailInnerBOList) throws ConsoleException
	{
		logger.info("====保存用户详细(内部)信息列表开始====");
		logger.debug("====保存用户详细(内部)信息列表.入参:UserDetailInnerBOList=[{}]====", userDetailInnerBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserDetailInnerBO itemBO : userDetailInnerBOList)
		{
			ConsoleReturnBO itemResult = saveUserDetailInner(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====保存用户详细(内部)信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存用户详细(内部)信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getUserDetailInner(UserDetailInnerInfo userDetailInnerInfo)
	{
		logger.info("====获取用户详细(内部)信息开始====");
		logger.debug("====获取用户详细(内部)信息.入参:UserDetailInnerInfo=[{}]====", userDetailInnerInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<UserDetailInnerPO> userDetailInnerPOList = userDetailInnerDao.select(userDetailInnerInfo);
		if (userDetailInnerPOList == null || userDetailInnerPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (userDetailInnerPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			UserDetailInnerBO userDetailInnerBO = new UserDetailInnerBO();
			copyUserDetailInnerPO2UserDetailInnerBO(userDetailInnerPOList.get(0), userDetailInnerBO);
			returnConsoleReturnBO.setReturnObj(userDetailInnerBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			UserDetailInnerBO userDetailInnerBO = new UserDetailInnerBO();
			copyUserDetailInnerPO2UserDetailInnerBO(userDetailInnerPOList.get(0), userDetailInnerBO);
			returnConsoleReturnBO.setReturnObj(userDetailInnerBO);
		}
		logger.debug("====获取用户详细(内部)信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取用户详细(内部)信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listUserDetailInner(UserDetailInnerInfo userDetailInnerInfo)
	{
		logger.info("====获取用户详细(内部)信息列表开始====");
		logger.debug("====获取用户详细(内部)信息列表.入参:UserDetailInnerInfo=[{}]====", userDetailInnerInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<UserDetailInnerPO> userDetailInnerPOList = userDetailInnerDao.select(userDetailInnerInfo);
		if (userDetailInnerPOList == null || userDetailInnerPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<UserDetailInnerBO> resultList = new ArrayList<UserDetailInnerBO>();
			for (UserDetailInnerPO itemPO : userDetailInnerPOList)
			{
				UserDetailInnerBO itemBO = new UserDetailInnerBO();
				copyUserDetailInnerPO2UserDetailInnerBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取用户详细(内部)信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取用户详细(内部)信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateUserDetailInner(UserDetailInnerBO userDetailInnerBO)
	{
		logger.info("====更新用户详细(内部)信息开始====");
		logger.debug("====更新用户详细(内部)信息.入参:UserDetailInnerBO=[{}]====", userDetailInnerBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserDetailInnerPO userDetailInnerPO = new UserDetailInnerPO();
		copyUserDetailInnerBO2UserDetailInnerPO(userDetailInnerBO, userDetailInnerPO);
		Boolean updateResult = userDetailInnerDao.update(userDetailInnerPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新用户详细(内部)信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新用户详细(内部)信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateUserDetailInner(List<UserDetailInnerBO> userDetailInnerBOList) throws ConsoleException
	{
		logger.info("====更新用户详细(内部)信息列表开始====");
		logger.debug("====更新用户详细(内部)信息列表.入参:List<UserDetailInnerBO>=[{}]====", userDetailInnerBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserDetailInnerBO itemBO : userDetailInnerBOList)
		{
			ConsoleReturnBO itemResult = updateUserDetailInner(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====更新用户详细(内部)信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新用户详细(内部)信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeUserDetailInner(UserDetailInnerBO userDetailInnerBO)
	{
		logger.info("====删除用户详细(内部)信息开始====");
		logger.debug("====删除用户详细(内部)信息.入参:UserDetailInnerBO＝[{}]======", userDetailInnerBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserDetailInnerPO userDetailInnerPO = new UserDetailInnerPO();
		copyUserDetailInnerBO2UserDetailInnerPO(userDetailInnerBO, userDetailInnerPO);
		Boolean deleteResult = userDetailInnerDao.delete(userDetailInnerPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除用户详细(内部)信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除用户详细(内部)信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeUserDetailInner(List<UserDetailInnerBO> userDetailInnerBOList) throws ConsoleException
	{
		logger.info("====删除用户详细(内部)信息列表开始====");
		logger.debug("====删除用户详细(内部)信息列表.入参:List<UserDetailInnerBO>＝[{}]======", userDetailInnerBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserDetailInnerBO itemBO : userDetailInnerBOList)
		{
			ConsoleReturnBO itemResult = removeUserDetailInner(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====删除用户详细(内部)信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除用户详细(内部)信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countUserDetailInner(UserDetailInnerBO userDetailInnerBO)
	{
		logger.info("====统计用户详细(内部)信息开始====");
		logger.debug("====统计用户详细(内部)信息。入参：UserDetailInnerBO＝[{}]======", userDetailInnerBO);
		Long returnLong = new Long(0);
		UserDetailInnerPO userDetailInnerPO = new UserDetailInnerPO();
		copyUserDetailInnerBO2UserDetailInnerPO(userDetailInnerBO, userDetailInnerPO);
		returnLong = userDetailInnerDao.count(userDetailInnerPO);
		logger.debug("====统计用户详细(内部)信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计用户详细(内部)信息结束====");
		return returnLong;
	}

	private void copyUserDetailInnerBO2UserDetailInnerPO(UserDetailInnerBO userDetailInnerBO, UserDetailInnerPO userDetailInnerPO)
	{
		beanUtils.copyBeanNotNull(userDetailInnerBO, userDetailInnerPO);
		if (userDetailInnerBO.getSysID() != null)
		{
			userDetailInnerPO.setSeqID(userDetailInnerBO.getSysID());
		}
	}

	private void copyUserDetailInnerPO2UserDetailInnerBO(UserDetailInnerPO userDetailInnerPO, UserDetailInnerBO userDetailInnerBO)
	{
		beanUtils.copyBeanNotNull(userDetailInnerPO, userDetailInnerBO);
		if (userDetailInnerPO.getSeqID() != null)
		{
			userDetailInnerBO.setSysID(userDetailInnerPO.getSeqID());
		}
	}

}
